package com.scorpio.controller.agentuser;

import com.alibaba.fastjson.JSONObject;
import com.scorpio.agentuser.domain.AgentUserEntity;
import com.scorpio.agentuser.service.AgentIncomeService;
import com.scorpio.agentuser.service.AgentRegUserService;
import com.scorpio.agentuser.service.AgentUserService;
import com.scorpio.agentuser.vo.CreateUserVo;
import com.scorpio.agentuser.vo.UpdateIncomeScaleVo;
import com.scorpio.agentuser.vo.UpdatePasswordVo;
import com.scorpio.agentuser.vo.UpdateUserVo;
import com.scorpio.common.annotation.RepeatSubmit;
import com.scorpio.common.core.controller.BaseController;
import com.scorpio.common.core.domain.Result;
import com.scorpio.common.exception.CustomException;
import com.scorpio.security.annotation.PersonalLogin;
import com.scorpio.security.annotation.PersonalLoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/api/user")
public class AgentUserController extends BaseController {

    @Autowired
    private AgentUserService personalService;

    @Autowired
    private AgentIncomeService incomeServer;

    @Autowired
    private AgentRegUserService regUserServer;

    @RepeatSubmit
    @PersonalLogin
    @PostMapping("createUser")
    public Result createUser(@PersonalLoginUser AgentUserEntity agent, @Validated @RequestBody CreateUserVo vo) {
        return toResult(personalService.createUser(agent, vo));
    }

    @RepeatSubmit
    @PersonalLogin
    @PostMapping("updateUser")
    public Result updateUser(@PersonalLoginUser AgentUserEntity agent, @Validated @RequestBody UpdateUserVo vo) {
        vo.setId(agent.getId());
        return toResult(personalService.updateUser(vo));
    }

    @PersonalLogin
    @GetMapping("loginInfo")
    public Result loginInfo(@PersonalLoginUser AgentUserEntity agent, String details) {
        if ("0".equals(details)) {
            return Result.success(personalService.selectDetailsById(agent.getId()));
        } else {
            return Result.success(agent);
        }
    }

    @PersonalLogin
    @PostMapping("updateIncomeScale")
    public Result updateIncomeScale(@PersonalLoginUser AgentUserEntity agent, @Validated @RequestBody UpdateIncomeScaleVo vo) {
        return Result.success(personalService.updateIncomeScale(agent, vo));
    }

    @RepeatSubmit
    @PersonalLogin
    @PostMapping("updatePassword")
    public Result updatePassword(@PersonalLoginUser AgentUserEntity agent, @Validated @RequestBody UpdatePasswordVo vo) {
        return Result.success(personalService.updatePassword(agent, vo));
    }

    @PersonalLogin
    @GetMapping("lowerUserList")
    public Result lowerUserList(@PersonalLoginUser AgentUserEntity agent) {
        startPage();
        return Result.success(personalService.lowerUserList(agent));
    }

    @PersonalLogin
    @GetMapping("detailsStatistics")
    public Result detailsStatistics(@PersonalLoginUser AgentUserEntity agent, @RequestParam(required = false) Long agentId, Integer type, Date quertDate) {
        JSONObject json = new JSONObject();
        AgentUserEntity entity = personalService.selectById(agentId);
        if (entity == null) {
            throw new CustomException("用户不存在或已删除");
        }
        json.put("underling", agent.getId().equals(entity.getPid()) ? 0 : 1);
        json.put("directScale", entity.getDirectScale());
        json.put("indirectScale", entity.getIndirectScale());
        json.put("income", incomeServer.statistics(agentId, type, quertDate));
        json.put("regUser", regUserServer.statistics(agentId, type, quertDate));
        return Result.success(json);
    }

}
